<?php

/**

 * 公告通知
 * 
 * @access  public 
 * @author  zhangyingheng<zhang_yh@jsumt.com.cn> 
 */

namespace Index\Controller;

use Think\Controller;

class NoticeController extends BaseController {

    public function _initialize() {
        parent::_initialize();
        $this->title = '公告通知';

        //获取公告分类
        $map['PARENT_NO'] = 'NOTIFY';
        $this->cates = M('sys_code')->where($map)->order('CODE_NO')->select();
    }

    public function index() {
        //未读公告
        $this->unread = count($this->getNoticeData('', 0));

        //已读公告
        $this->read = count($this->getNoticeData('', 1));

        $this->display();
    }

    //获取公告列表
    private function getNoticeData($limit = '', $type = '', $where = '') {
        if ($type == 2) {
            if (session('user_id') == 'lishaopu') {
                $where['PUBLISH'] = 2;
                $where['FROM_ID'] = session('user_id');
                $where['_logic'] = 'or';
                $map['_complex'] = $where;
            } else {
                $map['FROM_ID'] = session('user_id');
            }
        } elseif ($type != 2) {
            $map = "(TO_ID = 'ALL_DEPT' or TO_ID like '%" . session('dept_id') . "%'";
            $map .= " or PRIV_ID like '%" . session('user_priv') . "%'";
            $map .= " or USER_ID like '%" . session('user_id') . "%')";
            if ($type == 0) {
                $map .= " and READERS not like '%" . session('user_id') . "%'";
            }
            if ($type == 1) {
                $map .= " and READERS like '%" . session('user_id') . "%'";
            }
            $map .= " and PUBLISH = 1";
        }

        if ($where) {
            $map = $where;
        }

        $p = I('get.p', 1, 'intval');
        $start = $limit * ($p - 1);
        if ($limit) {
            $lists = M('notify')->field('FROM_ID,NOTIFY_ID,SUBJECT,SEND_TIME,PUBLISH,AUDITER')->where($map)->limit($start, $limit)->order('TOP DESC,SEND_TIME DESC')->select();
        } else {
            $lists = M('notify')->field('FROM_ID,NOTIFY_ID,SUBJECT,SEND_TIME,PUBLISH,AUDITER')->where($map)->order('TOP DESC,SEND_TIME DESC')->select();
        }
        return $lists;
    }

    //列表
    public function lists() {
        $limit = $this->limit;
        $lists = $this->getNoticeData($limit, I('type'));
        
        foreach($lists as $key=>$row){
            $lists[$key]['time'] = date('Y/m/d',strtotime($row['send_time']));
        }
        
        $this->type = I('type');
        if (I('type') == 2) {
            $this->auditer = session('user_id');
        }

        $this->lists = $lists;
        $this->type = I('type');
        $this->display();
    }

    //下拉获取更多
    public function dropload() {
        $data = I('get.');
        $limit = $this->limit;
        $lists = $this->getNoticeData($limit, $type);
        foreach ($lists as $key => $row) {
            $lists[$key]['type'] = I('type');
            $lists[$key]['verify'] = $row['publish'];
            $lists[$key]['publish'] = getNoticeStatus($row['publish']);
            $lists[$key]['auditer'] = session('user_id');
        }

        $this->ajaxReturn($lists);
    }

    //新建公告
    public function saveNotice() {
        if (IS_AJAX) {
            $data = I('post.');
            $data['BEGIN_DATE'] = $data['BEGIN_DATE'] ? strtotime($data['BEGIN_DATE']) : time();
            $data['END_DATE'] = $data['END_DATE'] ? strtotime($data['END_DATE']) : 0;
            $data['TO_ID'] = $data['dept_ids'] ? implode(',', $data['dept_ids']) : 'ALL_DEPT';
            $data['PRIV_ID'] = implode(',', $data['priv_id']);
            $data['USER_ID'] = implode(',', $data['participator']);
            $data['TOP'] = $data['TOP'] ? 1 : 0;

            //提交审批
            if (!I('get.type')) {
                $data['AUDITER'] = 'lishaopu';
                $data['PUBLISH'] = 2;
            } else {
                $data['PUBLISH'] = 0;
            }

            if ($data['id']) {
                $row = M('notify')->where('NOTIFY_ID=' . $data['id'])->find();
                if ($data['uploads_url'] && $data['uploads_url'] != $row['uploads_url']) {
                    @unlink('.' . $row['uploads_url']);
                } else {
                    $data['uploads_url'] = $row['uploads_url'];
                }
                $data['LAST_EDITOR'] = session('user_id');
                $data['LAST_EDIT_TIME'] = date('Y-m-d H:i:s');
                $res = M('notify')->where('NOTIFY_ID=' . $data['id'])->save($data);
            } else {
                $data['FROM_DEPT'] = session('dept_id');
                $data['FROM_ID'] = session('user_id');
                $data['PRINT'] = 1;
                $data['DOWNLOAD'] = 1;
                $data['SEND_TIME'] = date('Y-m-d H:i:s');

                $res = M('notify')->add($data);
            }
            if ($res !== false) {
                $return['info'] = '提交成功';
                $return['url'] = U('lists', array('type' => 2));
            } else {
                $return['info'] = '提交失败';
            }
            $this->ajaxReturn($return);
        }

        if (I('notify_id')) {
            $info = M('notify')->where('NOTIFY_ID=' . I('notify_id'))->find();
            if ($info['to_id'] != 'ALL_DEPT') {
                $this->to_ids = explode(',', $info['to_id']);
            }
            if ($info['priv_id']) {
                $this->priv_ids = explode(',', $info['priv_id']);
            }
            if ($info['user_id']) {
                $this->user_ids = explode(',', $info['user_id']);
            }
            
            //获取pc端上传的附件
            if($info['attachment_name']){
                $pcNames = downloadFile($info['attachment_id'],$info['attachment_name'],'notify');
            }
            
            //获取移动端上传的附件
//            if ($info['uploads_url']) {
//                $tmp1 = explode(';', $info['uploads']);
//                $tmp2 = explode(';', $info['uploads_url']);
//                foreach ($tmp1 as $key => $row) {
//                    if ($row) {
//                        $uploads[$key]['name'] = $row;
//                        $uploads[$key]['url'] = $tmp2[$key];
//                    }
//                }
//
//                $this->uploads = $uploads;
//            }

            $this->id = I('notify_id');
            $this->info = $info;
        }

        $this->display();
    }

    //查看公告
    public function show() {
        $map['NOTIFY_ID'] = I('notify_id');
        $info = M('notify')->where($map)->find();
        $info['content'] = str_replace(' ', "<br/>", $info['content']);
        //获取pc端附件
        if ($info['attachment_name']) {
            $uploads = downloadFile($info['attachment_id'],$info['attachment_name'],'notify');
        }
        //获取移动端附件
        if($info['uploads_url']){
            $names = explode(';', rtrim($info['uploads'],';'));
            $urls = explode(';', rtrim($info['uploads_url'],';'));
            for ($i = 0; $i < count($names); $i++) {
                if ($names[$i]) {
                    $uploads[$i]['name'] = $names[$i];
                    $uploads[$i]['url'] = $urls[$i];
                }
            }
        }

        //添加阅读人
        if ($info['readers']) {
            $data['READERS'] = $info['readers'] . session('user_id') . ',';
        } else {
            $data['READERS'] = session('user_id') . ',';
        }
        M('notify')->where($map)->save($data);

        $this->type = I('type');
        $this->uploads = $uploads;
        $this->info = $info;
        $this->display();
    }

    //提交审批
    public function verifyNotice() {
        $data = I('post.');
        $data['TOP'] = $data['TOP'] ? 1 : 0;
        $data['BEGIN_DATE'] = strtotime($data['BEGIN_DATE']); 
        $data['END_DATE'] = strtotime($data['END_DATE']);
        $data['AUDIT_DATE'] = date('Y-m-d');
        $map['NOTIFY_ID'] = $data['notify_id'];
        $res = M('notify')->where($map)->save($data);
        $return = $res !== false ? true : false;
        $this->ajaxReturn($return);
    }
    
    //删除公告
    public function delNotice() {
        $notify_id = I('notice_id');
        $res = M('notify')->where('NOTIFY_ID=' . $notify_id)->delete();
        if ($res) {
            $return['info'] = '删除成功';
        } else {
            $return['info'] = '删除失败';
        }
        $this->ajaxReturn($return);
    }

    //查询页面
    public function search() {
        if (IS_POST) {
            $data = I('post.');
            if ($data['from_id']) {
                $where['FROM_ID'] = M('user')->where('USER_NAME="' . $data['from_id'] . '"')->getField('user_id');
            }
            if ($data['type_id']) {
                $where['TYPE_ID'] = $data['type_id'];
            }
            if ($data['subject']) {
                $where['SUBJECT'] = array('like', '%' . $data['subject'] . '%');
            }
            if ($data['start'] && $data['end']) {
                $where['SEND_TIME'] = array('between', [$data['start'], $data['end']]);
            }

            $data = $this->getNoticeData('', '', $where);
            $this->ajaxReturn($data);
        } else {
            $this->display();
        }
    }

    /*
     * 获取上一篇或下一篇公告
     *
     */

    public function getOtherNotify() {
        $type = I('type');
        $id = I('id');
        $status = I('status');
        $lists = $this->getNoticeData('', $type);
        $len = count($lists) - 1;
        if ($type) {
            foreach ($lists as $key => $row) {
                if ($row['notify_id'] == $id) {
                    if ($status == 'prev') {
                        //取前一条
                        $notify = $key != 0 ? $lists[$key - 1] : [];
                    } else {
                        $notify = $key < $len ? $lists[$key + 1] : [];
                    }
                }
            }
        } else {
            foreach ($lists as $key => $row) {
                if ($key == 0 && ($row['notify_id'] < $id)) {
                    $arr['notify_id'] = $id;
                    array_unshift($lists, $arr);
                } else if (($key == $len) && ($row['notify_id'] > $id)) {
                    $arr['notify_id'] = $id;
                    array_push($lists, $arr);
                } else if ($row['notify_id'] > $id && $lists[$key + 1]['notify_id'] < $id) {
                    $arr[]['notify_id'] = $id;
                    array_splice($lists, $key + 1, 0, $arr);
                }
            }

            foreach ($lists as $key => $row) {
                if ($row['notify_id'] == $id) {
                    if ($status == 'prev') {
                        //取前一条
                        $notify = $key != 0 ? $lists[$key - 1] : [];
                    } else {
                        $notify = $key < $len ? $lists[$key + 1] : [];
                    }
                }
            }
        }

        if (empty($notify)) {
            $return['status'] = 0;
            $return['info'] = $status == 'prev' ? '已经是第一条公告' : '已经是最后一条公告';
        } else {
            $return['url'] = U('show', array('notify_id' => $notify['notify_id'], 'type' => $type));
            $return['id'] = $notify['notify_id'];
            $return['status'] = 1;
        }
        $this->ajaxReturn($return);
    }

    public function upload() {
        $this->upfile('./Public/Uploads/notice/');
    }

}
